package com.gy.paidao.person;

import android.os.Bundle;
import android.text.Editable;
import android.text.Html;
import android.text.Spannable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.gy.paidao.BaseActivity;
import com.example.mapdemo.R;
import com.gy.paidao.bean.BaseBean;
import com.gy.paidao.util.AnimUtil;
import com.gy.paidao.util.KeyBoard;
import com.gy.paidao.util.LogUtil;
import com.gy.paidao.util.ShowDialogUtil;
import com.gy.paidao.util.Urls;
import com.jky.struct2.http.core.AjaxParams;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 作者：gy on 16/2/21
 * 说明：忘记密码
 */
public class ForgetActivity extends BaseActivity {

    private ImageView imgBack,ingRight;
    private LinearLayout llFirst,llTwo,llPas;
    private EditText etEmail,etCode,etPwdOne,etPwdAgain;
    private TextView tvSendCode,tvResend,tvValidate,tvSet;
    private ProgressBar progressBar;
    private String strEmail,strCode,strPwdOne,strPwdAgain;
    private boolean sendCodeFlag;//是否可以点击发送验证码

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forget_layout);
        setViews();
    }

    @Override
    protected void initVariable() {

    }

    @Override
    protected void setTitleViews() {

    }

    @Override
    protected void setViews() {
        imgBack = (ImageView) findViewById(R.id.activity_forget_back);
        ingRight = (ImageView) findViewById(R.id.activity_forget_img_right);
        progressBar = (ProgressBar) findViewById(R.id.activity_forget_progress);
        llFirst = (LinearLayout) findViewById(R.id.activity_forget_ll_first);
        llTwo = (LinearLayout) findViewById(R.id.activity_forget_ll_second);
        llPas = (LinearLayout) findViewById(R.id.activity_forget_ll_set_pas);
        etEmail = (EditText) findViewById(R.id.activity_forget_et_email);
        etCode = (EditText) findViewById(R.id.activity_forget_et_code);
        etPwdOne = (EditText) findViewById(R.id.activity_forget_et_pas_one);
        etPwdAgain = (EditText) findViewById(R.id.activity_forget_et_pas_again);
        tvSendCode = (TextView) findViewById(R.id.activity_forget_tv_send_code);
        tvResend = (TextView) findViewById(R.id.activity_forget_tv_resend);
        tvValidate = (TextView) findViewById(R.id.activity_forget_tv_validate);
        tvSet = (TextView) findViewById(R.id.activity_forget_tv_set);

        imgBack.setOnClickListener(this);
        tvSendCode.setOnClickListener(this);
        tvResend.setOnClickListener(this);
        tvValidate.setOnClickListener(this);
        tvSet.setOnClickListener(this);

        etEmail.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if(isEmail(s.toString())){
                    sendCodeFlag = true;
                    tvSendCode.setBackgroundResource(R.drawable.shape_input_blue_bg);
                    tvSendCode.setTextColor(ForgetActivity.this.getResources().getColor(R.color.white));
                } else {
                    sendCodeFlag = false;
                    tvSendCode.setTextColor(0xff8dbedd);
                    tvSendCode.setBackgroundResource(R.drawable.shape_input_blue_alpha_bg);
                }
            }
        });
    }

    @Override
    protected void doClickAction(int viewId) {
        super.doClickAction(viewId);
        switch (viewId){
            case R.id.activity_forget_back:
                this.finish();
                AnimUtil.pushRightInAndOut(this);
                break;
            case R.id.activity_forget_tv_send_code://发送验证码
                if(sendCodeFlag){//邮箱格式正确
                    KeyBoard.demissKeyBoard(this,etCode);
                    strEmail = etEmail.getText().toString().trim();
                    dorequestCode();
                }
                break;
            case R.id.activity_forget_tv_resend://重新发送
                dorequestCode();
                break;
            case R.id.activity_forget_tv_validate://验证
                strCode = etCode.getText().toString().trim();
                if(TextUtils.isEmpty(strCode)){
                    showToast("请输入验证码");
                } else {
                    KeyBoard.demissKeyBoard(this,etCode);
                    progressBar.setVisibility(View.VISIBLE);
                    dorequestValidate();
                }
                break;
            case R.id.activity_forget_tv_set://设置密码
                strPwdOne = etPwdOne.getText().toString().trim();
                strPwdAgain = etPwdAgain.getText().toString().trim();
                if(TextUtils.isEmpty(strPwdOne) || TextUtils.isEmpty(strPwdAgain)){
                    showToast("请输入密码");
                } else if(!strPwdOne.equals(strPwdAgain)){
                    showToast("两次密码输入不一致");
                } else {
                    KeyBoard.demissKeyBoard(this,etCode);
                    String content = this.getResources().getString(R.string.forget_password_resetpassword);
                    Spannable span = (Spannable) Html.fromHtml(content);
                    ShowDialogUtil.showDialog(this,R.string.forget_password_resetpassword,span,
                            R.string.yes,R.string.cancel,this);
                }
                break;
            case R.id.dialog_show_money_btn_ok://确认
                dorequestSetNew();
                break;
        }
    }

    /** 忘记密码发送验证码*/
    private void dorequestCode(){
        showLoading();
        params = new AjaxParams();
        params.put("email",strEmail);
        httpRequest.post(Urls.url_sendvericodeforresetpwd, params, callBack, 0);
    }

    /** 验证验证码*/
    private void dorequestValidate(){
        showLoading();
        params = new AjaxParams();
        params.put("email",strEmail);
        params.put("verificationCode",strCode);
        httpRequest.post(Urls.url_checkvericodeforresetpwd, params, callBack, 1);
    }

    /** 设置新密码*/
    private void dorequestSetNew(){
        showLoading();
        params = new AjaxParams();
        params.put("email",strEmail);
        params.put("verificationCode",strCode);
        params.put("newPwd",strPwdOne);
        httpRequest.post(Urls.url_updatepwd, params, callBack, 2);
    }

    @Override
    protected void handlerBean(int requestCode, BaseBean baseBean) {
        super.handlerBean(requestCode, baseBean);
        LogUtil.e(baseBean.toString());
        if(!baseBean.success){
            showToast(baseBean.errMsg);
        }
        switch (requestCode){
            case 0://忘记密码发送验证码
                if(baseBean.success){
                    llFirst.setVisibility(View.GONE);
                    llTwo.setVisibility(View.VISIBLE);
                }
                break;
            case 1:
                llPas.setVisibility(View.VISIBLE);
                progressBar.setVisibility(View.GONE);
                ingRight.setVisibility(View.VISIBLE);
                break;
            case 2:
                if(baseBean.success){
                    ForgetActivity.this.finish();
                    AnimUtil.pushRightInAndOut(this);
                }
                break;
        }
    }

    /** 判断邮箱格式是否合法*/
    public boolean isEmail(String email) {
        String str = "^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$";
        Pattern p = Pattern.compile(str);
        Matcher m = p.matcher(email);

        return m.matches();
    }
}
